package com.msmall.community.vote.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.Constants;
import com.msmall.community.vote.dao.VoteDao;
import com.msmall.community.vote.model.Vote;
import com.msmall.community.vote.model.VoteModule;
import com.msmall.community.vote.service.VoteManager;

public class VoteManagerImpl extends BaseManagerImpl<Vote, Long> implements VoteManager{

	private static final Log log = LogFactory.getLog(VoteManagerImpl.class);
	
	private VoteDao voteDao;

	public void setVoteDao(VoteDao voteDao) {
		this.voteDao = voteDao;
	}

	public void addVote(Vote vote, Long userid) {
		try{
			vote.setVoter(userid);
			voteDao.saveObject(vote);
		}catch(Exception e){
			log.error(e);
		}
	}
	/**
	 * 获得voter对 voted的最近一次投票信息，可以用来防止刷票
	 * @param voter
	 * @param voted
	 */
	public Vote loadLastVote(Long voter, Long voted, Integer module){
		try{
			return voteDao.loadLastVote(voter, voted, module);
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}
	/**
	 * 从缓存获得旅游达人的投票前10名 ,只有被投票人ID，和受到的投票数量
	 */
	public List<VoteModule> getTravelVote(){
		try{
			List<VoteModule> list = (List<VoteModule>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_VOTE_TRAVEL_LIST);
			return list;
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}
	/**
	 * 查看用户在某个模块受到的所有投票数量
	 */
	public Integer loadTotalVote(Long voted, Integer module, Integer types){
		try{
			return voteDao.loadTotalVote(voted, module, types);
		}catch(Exception e){
			log.error(e);
			return 0;
		}
	}
	/**
	 * 获得旅游达人的投票前10名 ,只有被投票人ID，和受到的投票数量
	 */
	public List loadTravelVote() {
		try{
			return voteDao.loadTravelVote();
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}
	
	
}
